/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package libPackage;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import java.awt.Point;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author duonghau
 */
public class CommonFunction {
    DFAgentDescription template;
    ServiceDescription sd;
    public void registerService(Agent agent,String serviceName){        
        DFAgentDescription dfd = new DFAgentDescription();
        ServiceDescription serviceDescription = new ServiceDescription();
        dfd.setName(agent.getAID());        
        serviceDescription.setType(serviceName);
        serviceDescription.setName(serviceName);
        dfd.addServices(serviceDescription);
        try {
            DFService.register(agent, dfd);
        } catch (FIPAException fe) {
            fe.printStackTrace();
        }
    }
    public ArrayList<AID> getAgents(Agent agent, String typeAgent){
        ArrayList<AID> agents=new ArrayList<>();
        template = new DFAgentDescription();
        sd = new ServiceDescription();
        sd.setType(typeAgent);
        template.addServices(sd);
        try {
            DFAgentDescription[] result = DFService.search(agent, template);
            for (int i = 0; i < result.length; ++i) {
                 AID agentAID = result[i].getName();
                 agents.add(agentAID);
            }
        }catch (FIPAException fe){
            fe.printStackTrace(); 
        }        
        return agents;
    }
    
    public ArrayList<AID> getAllAgents(Agent agent){
        ArrayList<AID> agents=new ArrayList<>();
        agents.addAll(getAllHerbivores(agent));
        agents.addAll(getAllHommes(agent));
        agents.addAll(getAllPlantes(agent));
        agents.addAll(getAlCarnivores(agent));
        return agents;
    }
    
    public ArrayList<AID> getAllHerbivores(Agent agent){
        return getAgents(agent, "herbivore");
    }
    
    public ArrayList<AID>  getAllHommes(Agent agent){
        return getAgents(agent,"homme");
    }
    
    public ArrayList<AID>  getAllPlantes(Agent agent){
        return getAgents(agent,"plante");
    }
    
    public ArrayList<AID>  getAlCarnivores(Agent agent){
        return getAgents(agent,"carnivore");
    }   
    
    public double getDistance(Point start, Point destination){        
        return Math.sqrt((start.x - destination.x)*(start.x - destination.x) + 
                (start.y - destination.y)*(start.y - destination.y));
    }
    public boolean createNewAgent(AgentContainer agentContainter,String agentName,String agentClass){
        //AgentContainer c = getContainerController();
        try {
            AgentController a = agentContainter.createNewAgent( agentName, agentClass, null );
            a.start();
        }
        catch (Exception e){
            return false;
        } 
        return true;
    }
    public Properties getProperties(){
        Properties props = new Properties();
        try{
            props.load(new FileInputStream("config.properties"));
        }catch(Exception e){
            System.out.println("Error read config file");
        }
        return props;
    }
}
